package com.hdoit.ares.logical.claim.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.claim.dao.IClaimApplyforDao;
import com.hdoit.ares.logical.claim.model.ClaimApplyfor;
import com.hdoit.ares.logical.claim.model.ClaimHandler;
import com.hdoit.ares.logical.viewmodel.claim.ClaimApplyforSearch;
import com.hdoit.ares.selfcommon.enums.claim.ApplyforType;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月03日 09:03:48
 * @comment
 */
@Component
public class ClaimApplyforDaoImpl extends SuperDao<ClaimApplyfor> implements IClaimApplyforDao{

	@Override
	public List<ClaimApplyfor> queryList(
			ClaimApplyforSearch claimApplyforSearch, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT claimApplyfor.*","claimHandler.name as trackPeopleName","claimHandler.startDate as trackDate","claimHandler.state as trackState")
						.from(ClaimApplyfor.class,"claimApplyfor")
						.leftJoin(ClaimHandler.class,"claimHandler")
						.on("claimApplyfor.aid", "claimHandler.id")
						.andWhere("claimApplyfor.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.setMainTableAlias("claimApplyfor");
		
		appendFinder(claimApplyforSearch,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, claimApplyforSearch); 
          }
        return queryList(finderExpression, claimApplyforSearch, pagerModel);
	  }

	@Override
	public List<ClaimApplyfor> queryList(ClaimApplyforSearch claimApplyforSearch) {
		return queryList(claimApplyforSearch,null);
	}
	
	@Override
	public ClaimApplyfor queryModel(String key) throws DaoBaseException {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("claimApplyfor.*","claimHandler.name as trackPeopleName","claimHandler.startDate as trackDate","claimHandler.state as trackState")
						.from(ClaimApplyfor.class,"claimApplyfor")
						.leftJoin(ClaimHandler.class,"claimHandler")
						.on("claimApplyfor.aid", "claimHandler.id")
						.andWhere("claimApplyfor.id=#{claimId}")
						.setParam("claimId", key)
						.andWhere("claimApplyfor.deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no);
						
		return super.queryModel(finderExpression);
	}

	@Override
	public int queryCount(ClaimApplyforSearch claimApplyforSearch) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT claimApplyfor.id) as count")
						.from(ClaimApplyfor.class,"claimApplyfor")
						.leftJoin(ClaimHandler.class,"claimHandler")
						.on("claimApplyfor.aid", "claimHandler.id")
						.andWhere("claimApplyfor.deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no)
						.setMainTableAlias("claimApplyfor");
		
		appendFinder(claimApplyforSearch,finderExpression);
		return queryCount(finderExpression, claimApplyforSearch);
	}
    
	private void appendFinder(ClaimApplyforSearch claimApplyforSearch,IFinderExpression finder){
		if(!StringUtils.isEmpty(claimApplyforSearch.getApplyforName())){
			finder.andWhere("claimApplyfor.name like #{aname}")
				  .setParam("aname", "%"+claimApplyforSearch.getApplyforName()+"%");
		}
		
		if(!StringUtils.isEmpty(claimApplyforSearch.getTrackName())){
			finder.andWhere("claimHandler.name like #{hname}")
				  .setParam("hname", "%"+claimApplyforSearch.getTrackName()+"%");
		}
		
		if(claimApplyforSearch.getStartDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},claimApplyfor.createDate)>=0")
				  .setParam("startDate", claimApplyforSearch.getStartDate());
		}
		if(claimApplyforSearch.getEndDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},claimApplyfor.createDate)<=0")
				  .setParam("endDate", claimApplyforSearch.getEndDate());
		}
		
		if(!StringUtils.isEmpty(claimApplyforSearch.getProductName())){
			finder.andWhere("claimApplyfor.product=#{product}")
			  	  .setParam("product", claimApplyforSearch.getProductName());
		}
		
		if(!StringUtils.isEmpty(claimApplyforSearch.getCheckType())){
			finder.andWhere("claimApplyfor.state=#{astate}")
			  	  .setParam("astate", claimApplyforSearch.getCheckType());
		}
		if(!StringUtils.isEmpty(claimApplyforSearch.getTrackType())){
			finder.andWhere("claimHandler.state=#{hstate}")
		  	  	  .setParam("hstate", claimApplyforSearch.getTrackType());
		}
	}

	@Override
	public List<ClaimApplyfor> queryClaim(String custId, String state) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("id","state","orderNum","product","createDate")
						.from(ClaimApplyfor.class)
						.andEq("customerId", custId);
		
		if("finished".equals(state)){
			finderExpression.andEq("state",ApplyforType.Finished);
		}else if("underway".equals(state)){
			finderExpression.andEq("state", ApplyforType.Checking)
							.orWhere("state=#{checked}")
							.setParam("checked", ApplyforType.Checked);
		}
		
		finderExpression.andEq("deleteMark", YesOrNo.no)
						.addOrder("createDate", OrderEnum.desc);
	
		return queryList(finderExpression);
	}
	
	
	
	
}